package com.example.havi.adapter;

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.example.havi.R;
import com.example.havi.bean.EmojiBean;

import java.util.List;

public class EmojiAdapter extends RecyclerView.Adapter {

    private final Context context;
    List<EmojiBean> emojis;

    public EmojiAdapter(Context context,List<EmojiBean> emojis) {
        this.emojis = emojis;
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_emoji,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, final int position) {
        final ViewHolder holder1 = (ViewHolder) holder;
        final EmojiBean emojiBean = emojis.get(position);
        holder1.tv.setImageResource(emojiBean.getEmojiDrawableId());
        holder1.rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener!=null)
                    listener.onItemClick(holder1.rootView,position,emojiBean);
            }
        });
    }


    @Override
    public int getItemCount() {
        return emojis.size();
    }


    class ViewHolder extends RecyclerView.ViewHolder {
        ImageView tv;
        View rootView;
        public ViewHolder(View itemView) {
            super(itemView);
            tv = itemView.findViewById(R.id.image);
            rootView = itemView;
        }
    }

    private OnItemClickListener listener;

    public void setOnItemClickListener(OnItemClickListener l){
        listener = l;
    }

    public interface OnItemClickListener{
        void onItemClick(View view,int position,EmojiBean bean);
    }
}
